package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.Sharecartworeg;
import com.gljx.web.Entity.Sharecartworegentry;
import com.gljx.web.Entity.Student;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.SharecartworegMapper;
import com.gljx.web.dao.SharecartworegentryMapper;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.SharecartworegService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gljx.util.BigDecimalUtil.NullToBigDecimal;

/**
 * Created by Administrator on 2017/11/20 0020.
 */
@Transactional
@Service
public class SharecartworegServiceImpl extends CommonDao implements SharecartworegService {
    @Autowired
    SharecartworegMapper sharecartworegMapper;
    @Autowired
    SharecartworegentryMapper sharecartworegentryMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    CustomLogService customLogService;
    @Autowired
    HttpSession session;

    @Override
    public List<Sharecartworegentry> selectSharecartworegentryByFid(String fid, String cfstudentname) {
        return sharecartworegentryMapper.selectSharecartworegentryByFparentid(fid, cfstudentname);
    }

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.SharecartworegMapper.pagecount", paramMap, null);
        return paramMap;
    }

    /**
     * @Author lcx
     * @Date 2017/11/21 0021 上午 11:45
     * 根据教练的fid查询教练的 名称 手机 车型 车牌号 教练组 教练组编码 部门 部门id
     */
    @Override
    public Sharecartworegentry queryPersonCarByFid(String id) {
        return sharecartworegentryMapper.selectPersonCarByFid(id).get(0);
    }


    /**
     * @Author lcx
     * @Date 2017/11/23 0023 下午 2:14
     * 删除科目二分车
     */
    @Override
    public void deleteBySharecartworegKey(String[] deleteId, String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();
        User user = (User) session.getAttribute("user");
        for (int i = 0; i < deleteId.length; i++) {

            //添加学员服务日志
            List<Sharecartworegentry> sharecartworegentries = sharecartworegentryMapper.selectSharecartworegentryByFparentidAstwo(deleteId[i]);

            for (Sharecartworegentry sharecartworegentry : sharecartworegentries) {
                customLogService.addLog("科目二分车进行删除", "删除了学员:" +
                                sharecartworegentry.getCfstudentno() + sharecartworegentry.getXyname() + "科目二分车!申请车型:"
                                + sharecartworegentry.getCfsqzjcx() + "分车时间:" + sharecartworegentry.getFbizdate()
                                + "单据编号：" + sharecartworegentry.getFnumber(),
                        sharecartworegentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }

            actitiviUtil.deleteTask(ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid()) ? ActivitiKey.FNUMBER_KM2FC_GL : ActivitiKey.FNUMBER_KM2FC_NC, fnumber[i]);
            sharecartworegMapper.deleteByPrimaryKey(deleteId[i]);
            sharecartworegentryMapper.deleteByFparentid(deleteId[i]);
        }
    }

    /**
     * @Author lcx
     * @Date 2017/11/23 0023 下午 2:39
     * 查询科目二分车信息
     */
    @Override
    public Sharecartworeg selectSharecartworegByFid(String fid) {

        Sharecartworeg sharecartworeg = new Sharecartworeg();
        sharecartworeg = sharecartworegMapper.selectByPrimaryKey(fid);
        //此方法查询的内容不是外面列表查询的内容是点击查看的内容
        if (sharecartworeg != null) {
            sharecartworeg.setSharecartworegentryList(sharecartworegentryMapper.selectSharecartworegentryByFparentidAstwo(fid));
        }

        return sharecartworeg;
    }

    @Override
    public void addsharecartworeg(String state, Sharecartworeg sharecartworeg, String[] jlznamebm,
                                  String[] jlzname, String[] cfduizhang, String[] jlname,
                                  String[] cfcoachphone, String[] cfcarcx, String[] cfcarnumber,
                                  String[] cfstudentid, String[] xyname, String[] cfstudentno, String[] cfidcardno,
                                  String[] cfstuvip, String[] cfstubillstate, String[] cffristexam,
                                  String[] cfkeyhgsj, String[] cfsex, String[] cflastkssj, String[] cfkscs,
                                  String[] cfphone, String[] cfmobile, String[] cfsqzjcx, String[] cfbdyd,
                                  String[] cfpxlx, String[] cftjr, String[] cfclazz, String[] cfyishou,
                                  String[] cfysje, String[] cfyouhui, String[] cfqianfei, String[] cfremark) {
        //删除老的信息在次添加
        if ("1".equals(state)) {
            //查询之前的信息
            Sharecartworeg sharecartworegtem = sharecartworegMapper.selectByPrimaryKey(sharecartworeg.getFid());
            sharecartworeg.setFcreatorid(sharecartworegtem.getFcreatorid());
            sharecartworeg.setFcreatetime(sharecartworegtem.getFcreatetime());
            //sharecartworegMapper.deleteByPrimaryKey(sharecartworeg.getFid());
            sharecartworegentryMapper.deleteByFparentid(sharecartworeg.getFid());
            //为了消除提交页面点击确定丢失审核人的 问题
            if ("2".equals(sharecartworeg.getFfivouchered() + "")) {
                sharecartworeg.setFauditorid(sharecartworegtem.getFauditorid());
                sharecartworeg.setCfbillstate(Long.valueOf("3"));
            }
        }

        /*获取uuid*/
        String uuid = GetFid.UUIDIntoFid();
        if (cfstudentid != null || cfstudentid.length != 0) {
            //添加子类
            System.out.println(cfstudentid.length);

            for (int i = 0; i < cfstudentid.length; i++) {
                if(jlzname[i] == null){
                    continue;
                }
                Sharecartworegentry sharecartworegentry = new Sharecartworegentry();

                sharecartworegentry.setFid(GetFid.UUIDIntoFid());
                if ("1".equals(state)) {
                    sharecartworegentry.setFparentid(sharecartworeg.getFid());
                } else {
                    sharecartworegentry.setFparentid(uuid);
                }
                sharecartworegentry.setCfjlgroupid(jlzname[i]);
                sharecartworegentry.setCfjsgroupno(jlznamebm[i]);
                sharecartworegentry.setCfcoachid(jlname[i]);
                //（0：是1:否）
                //sharecartworegentry.setCfduizhang(Long.valueOf(cfduizhang[i]));
                sharecartworegentry.setJlname(jlname[i]);
                sharecartworegentry.setCfcoachphone(cfcoachphone[i]);
                sharecartworegentry.setCfcarcx(cfcarcx[i]);
                sharecartworegentry.setCfcarnumber(cfcarnumber[i]);
                sharecartworegentry.setCfstudentid(cfstudentid[i]);
                sharecartworegentry.setCfstudentno(cfstudentno[i]);
                sharecartworegentry.setCfidcardno(cfidcardno[i]);
                //（0：是1:否）
                sharecartworegentry.setCfstuvip(cfstuvip[i] == null ? 1 : Long.valueOf(cfstuvip[i]));
                sharecartworegentry.setCfstubillstate(Long.valueOf(cfstubillstate[i]));
                sharecartworegentry.setCffristexam(cffristexam[i]);
                sharecartworegentry.setCfkeyhgsj(cfkeyhgsj[i]);
                if ("男".equals(cfsex[i])) {
                    sharecartworegentry.setCfsex(1L);
                } else {
                    sharecartworegentry.setCfsex(2L);
                }
                sharecartworegentry.setCfkscs(Long.valueOf("".equals(cfkscs[i]) ||  "null".equals(cfkscs[i])? "0":cfkscs[i]));
                sharecartworegentry.setCflastkssj(cflastkssj[i]);
                sharecartworegentry.setCfphone(cfphone[i]);
                sharecartworegentry.setCfmobile(cfmobile[i]);
                sharecartworegentry.setCfsqzjcx(cfsqzjcx[i]);
                if ("本地".equals(cfbdyd[i])) {
                    sharecartworegentry.setCfbdyd(1L);
                } else {
                    sharecartworegentry.setCfbdyd(2L);
                }
                sharecartworegentry.setCfpxlx(cfpxlx[i]);
                sharecartworegentry.setCftjr(cftjr[i]);
                sharecartworegentry.setCfclazz(cfclazz[i]);
                if (StringUtils.isNotEmpty(cfyishou[i])) {
                    sharecartworegentry.setCfyishou(new BigDecimal(NullToBigDecimal(cfyishou[i])));
                }
                if (StringUtils.isNotEmpty(cfysje[i])) {

                    sharecartworegentry.setCfysje(new BigDecimal(NullToBigDecimal(cfysje[i])));
                }
                if (StringUtils.isNotEmpty(cfyouhui[i])) {
                    sharecartworegentry.setCfyouhui(new BigDecimal(NullToBigDecimal(cfyouhui[i])));
                }
                if (StringUtils.isNotEmpty(cfqianfei[i])) {

                    sharecartworegentry.setCfqianfei(new BigDecimal(NullToBigDecimal(cfqianfei[i])));
                }
                sharecartworegentry.setCfremark(cfremark[i]);

                if ("6".equals(sharecartworeg.getFfivouchered() + "")) {
                    System.err.println("111");
                }

                sharecartworegentryMapper.insertSelective(sharecartworegentry);

                if ("1".equals(state)) {
                    //添加学员服务日志
                    customLogService.addLog("科目二分车进行修改", "修改了学员:" +
                                    sharecartworegentry.getCfstudentno() + xyname[i] + "科目二分车!申请车型:"
                                    + cfsqzjcx[i] + "分车时间:" + sharecartworeg.getFbizdate()
                                    + "单据编号：" + sharecartworeg.getFnumber(),
                            sharecartworegentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
                } else {
                    //添加学员服务日志
                    customLogService.addLog("科目二分车进行增加", "新增了学员:" +
                                    sharecartworegentry.getCfstudentno() + xyname[i] + "科目二分车!申请车型:" + cfsqzjcx[i]
                                    + "分车时间:" + sharecartworeg.getFbizdate()
                                    + "单据编号：" + sharecartworeg.getFnumber(),
                            sharecartworegentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
                }

            }

            //修复添加消息中心更新会调用新增方法的问题 修改为 调用更新 原因：之前做提交并且更改 的想法 后求需求不同 修改
            if ("1".equals(state)) {
                sharecartworegMapper.updateByPrimaryKeySelective(sharecartworeg);
            } else {
                sharecartworeg.setFid(uuid);
                sharecartworegMapper.insertSelective(sharecartworeg);

            }

        }


    }

    /**
     * @Author lcx
     * @Date 2018/2/23 0023 上午 11:03
     * studentId 学生id  cfstubillstateid 学生状态
     */
    @Override
    public void modifyStudentsState(String[] cfstudentid, String[] cfstubillstateid, String cflcsjid) {

        for (int i = 0; i < cfstudentid.length; i++) {
            Student student = new Student();
            student.setFid(cfstudentid[i]);
            //修改学生表中的练车时间id
            student.setCfkmelcsjid(cflcsjid);

            String cfstubillstateids = "";

            //如果是 批量审核则查询数据库 中的学生状态
            if (cfstubillstateid.length == 0){
                Student student1 = studentMapper.selectStudentById(cfstudentid[i]);
                if (student != null) {
                    cfstubillstateids = String.valueOf(student1.getCfstudentstate());
                }
            }
            else {
                cfstubillstateids = cfstubillstateid[i];
            }

            if ("2".equals(cfstubillstateids) || "5".equals(cfstubillstateids) || "7".equals(cfstubillstateids)) {
                student.setCfstudentstate(Long.valueOf("6"));
            }
            if ("17".equals(cfstubillstateids) || "19".equals(cfstubillstateids)) {
                student.setCfstudentstate(Long.valueOf("18"));
            }
            if ("20".equals(cfstubillstateids) || "21".equals(cfstubillstateids)) {
                student.setCfstudentstate(Long.valueOf("22"));
            }

            studentMapper.updateByPrimaryKeySelective(student);
        }
    }

    /**
     * @Author lcx
     * @Date 2018/3/9 0009 下午 5:34
     * 反审核修改学生状态
     */
    @Override
    public void modifyStudentsState2(String[] cfstudentid, String[] cfstubillstateid, String cflcsjid) {

        for (int i = 0; i < cfstudentid.length; i++) {
            Student student = new Student();
            student.setFid(cfstudentid[i]);
            //修改学生表中的练车时间id
            student.setCfkmelcsjid(cflcsjid);
            student.setCfstudentstate(Long.valueOf(cfstubillstateid[i]));

            studentMapper.updateByPrimaryKeySelective(student);
        }
    }

    /**
     * @Author lcx
     * @Date 2018/2/26 0026 下午 3:54
     * 添加服务日志 String state 1 提交 2 反审核
     */
    public void addLog(String fid, String state) {

        List<Sharecartworegentry> sharecartworegentries = sharecartworegentryMapper.selectSharecartworegentryByFparentid(fid, "");

        for (Sharecartworegentry sharecartworegentry : sharecartworegentries) {

            if ("1".equals(state)) {
                customLogService.addLog("科目二分车进行提交", "提交了学员:" +
                                sharecartworegentry.getCfstudentno() + sharecartworegentry.getXyname() + "科目二分车!申请车型:"
                                + sharecartworegentry.getCfsqzjcx() + "分车时间:" + sharecartworegentry.getFbizdate()
                                + "单据编号：" + sharecartworegentry.getFnumber(),
                        sharecartworegentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
            if ("2".equals(state)) {
                customLogService.addLog("科目二分车进行审核", "审核了学员:" +
                                sharecartworegentry.getCfstudentno() + sharecartworegentry.getXyname() + "科目二分车!申请车型:"
                                + sharecartworegentry.getCfsqzjcx() + "分车时间:" + sharecartworegentry.getFbizdate()
                                + "单据编号：" + sharecartworegentry.getFnumber(),
                        sharecartworegentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
            if ("3".equals(state)) {
                customLogService.addLog("科目二分车进行反审核", "反审核了学员:" +
                                sharecartworegentry.getCfstudentno() + sharecartworegentry.getXyname() + "科目二分车!申请车型:"
                                + sharecartworegentry.getCfsqzjcx() + "分车时间:" + sharecartworegentry.getFbizdate()
                                + "单据编号：" + sharecartworegentry.getFnumber(),
                        sharecartworegentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
        }
    }

    /**
     * @Author lcx
     * @Date 2018/3/5 0005 下午 3:21
     */
    @Override
    public Long selectStudentstateAsLcsjAndStudentId(String cflcsjid, String studentid) {


        return sharecartworegMapper.selectStudentstate(cflcsjid, studentid);
    }

    @Override
    public List<Sharecartworegentry> selectSharecartworegAsXls(String fnumber, String fdescription, String cfstudentname, String beginTime, String endTime, String fcompanyid) {
        return sharecartworegMapper.selectSharecartworegAsXls(fnumber, fdescription, cfstudentname, beginTime, endTime, fcompanyid);
    }

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.SharecartworegMapper.pagemap", paramMap, null);
        return paramMap;
    }
}
